﻿using GameHelper.Tools;
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using TowerDefence_Zombie.Data;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace TowerDefence_Zombie
{
    public class UIBoxShoopingFrom : UGuiFormEx
    {

        [SerializeField, Title("主界面退出按钮")] private Button mainMenuReturnBtn;
        [SerializeField, Title("当前金币")] private TextMeshProUGUI currentCoinText;
        [SerializeField, Title("当前钻石")] private TextMeshProUGUI currentDiamondText;
        [SerializeField, Title("当前通用材料")] private TextMeshProUGUI currentCommonMaterialText;

        [SerializeField, Title("普通宝箱按钮")] private Button normalBoxBtn;
        [SerializeField, Title("普通宝箱ID")] private int normalBoxId;
        [SerializeField, Title("精英宝箱按钮")] private Button eliteBoxBtn;
        [SerializeField, Title("精英宝箱ID")] private int eliteBoxId;

        [Title("开启界面"), SerializeField] private GameObject OpenPanel;
        [Title("开始界面")] public GameObject StartPanel;
        [Title("开始界面宝箱")] public Image StartTreasureImage;
        [Title("动画界面")] public GameObject AnimatePanel;
        [Title("动画界面宝箱")] public Image AnimateTreasureImage;
        [Title("动画卡片")] public TreasureProItem treasureProItem;
        [Title("结束界面")] public GameObject EndPanel;
        [Title("结束界面卡片节点")] public Transform EndCardNode;
        [Title("结束界面关闭按钮")] public Button EndCloseButton;
        [Title("卡片预制体")] public GameObject CardPrefab;
        private TreasureBoxData treasureBoxData;

        private List<MoneyInfo> moneyInfos = new();

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            var hoerUpgradeData = GameEntry.Data.GetData<HeroUpgradeData>();
            long curCommonMaterial = dataPlayer.GetCurrency(103);
            currentCommonMaterialText.text = ((HugeNum)curCommonMaterial).ToString();
            currentCoinText.text = ((HugeNum)dataPlayer.GetCurrency(CurrencyType.Coin)).ToString();
            currentDiamondText.text = ((HugeNum)dataPlayer.GetCurrency(CurrencyType.Gem)).ToString();
            RefreshBtnState();
            mainMenuReturnBtn.onClick.AddListener(CloseForm);
            normalBoxBtn.onClick.AddListener(OpenNormalBoxAnimatorPanel);
            EndCloseButton.onClick.AddListener(CloseBoxOpenPanel);
            eliteBoxBtn.onClick.AddListener(OpenEliteeBoxAnimatorPanel);
        }

        private void RefreshBtnState()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            if ((HugeNum)dataPlayer.GetCurrency(CurrencyType.Gem) >= 200)
                normalBoxBtn.interactable = true;
            else normalBoxBtn.interactable = false;

            if ((HugeNum)dataPlayer.GetCurrency(CurrencyType.Gem) >= 400)
                eliteBoxBtn.interactable = true;
            else eliteBoxBtn.interactable = false;
        }

        void OpenNormalBoxAnimatorPanel()
        {
            GameEntry.Data.GetData<DataLevel>().TryGetTreasureBoxData(normalBoxId, out treasureBoxData);
            StartTreasureImage.sprite = Resources.Load<Sprite>(treasureBoxData.IconResId);
            AnimateTreasureImage.sprite = Resources.Load<Sprite>(treasureBoxData.OpenIconRes);
            OpenPanel.SetActive(true);
            moneyInfos.Clear();
            for (int i = 0; i < treasureBoxData.MoneyInfos.Count; i++)
                moneyInfos.Add(new MoneyInfo(treasureBoxData.MoneyInfos[i].MoneyId, treasureBoxData.MoneyInfos[i].CountMin, treasureBoxData.MoneyInfos[i].CountMax));
            StartCoroutine(ProcessTreasure());
        }

        void OpenEliteeBoxAnimatorPanel()
        {
            GameEntry.Data.GetData<DataLevel>().TryGetTreasureBoxData(eliteBoxId, out treasureBoxData);
            StartTreasureImage.sprite = Resources.Load<Sprite>(treasureBoxData.IconResId);
            AnimateTreasureImage.sprite = Resources.Load<Sprite>(treasureBoxData.OpenIconRes);
            OpenPanel.SetActive(true);
            moneyInfos.Clear();
            for (int i = 0; i < treasureBoxData.MoneyInfos.Count; i++)
                moneyInfos.Add(new MoneyInfo(treasureBoxData.MoneyInfos[i].MoneyId, treasureBoxData.MoneyInfos[i].CountMin, treasureBoxData.MoneyInfos[i].CountMax));
            StartCoroutine(ProcessTreasure());
        }

        private IEnumerator ProcessTreasure()
        {
            StartPanel.SetActive(true);
            yield return new WaitForSeconds(1f);
            StartPanel.SetActive(false);
            AnimatePanel.SetActive(true);
            GameEntry.Sound.PlaySound(EnumSound.TreasureBoxMusic, null);
            for (int i = 0; i < moneyInfos.Count; i++)
            {
                treasureProItem.gameObject.SetActive(false);
                var MoneyInfo = moneyInfos[i];
                MoneyEntityData worldMoneyData = GameEntry.Data.GetData<MoneyEntity>().GetMoneyEntityData(MoneyInfo.MoneyId);
                treasureProItem.SetItem(worldMoneyData.Icon, worldMoneyData.Name, MoneyInfo.Count);
                treasureProItem.gameObject.SetActive(true);
                yield return new WaitForSeconds(1f);
            }
            yield return new WaitForSeconds(1f);
            AnimatePanel.SetActive(false);
            EndPanel.SetActive(true);
            for (int i = 0; i < moneyInfos.Count; i++)
            {
                var CardInfo = moneyInfos[i];
                MoneyEntityData worldMoneyData = GameEntry.Data.GetData<MoneyEntity>().GetMoneyEntityData(CardInfo.MoneyId);
                var card = Instantiate(CardPrefab, EndCardNode);
                card.SetActive(true);
                card.GetComponent<TreasureProItem>().SetItem(worldMoneyData.Icon, worldMoneyData.Name, CardInfo.Count);
            }
        }

        protected override void OnClose(bool isShutdown, object userData)
        {
            base.OnClose(isShutdown, userData);
            mainMenuReturnBtn.onClick.RemoveListener(CloseForm);
            normalBoxBtn.onClick.RemoveListener(OpenNormalBoxAnimatorPanel);
            EndCloseButton.onClick.RemoveListener(CloseBoxOpenPanel);
            eliteBoxBtn.onClick.RemoveListener(OpenEliteeBoxAnimatorPanel);
        }

        private void CloseBoxOpenPanel()
        {
            OpenPanel.SetActive(false);
            EndPanel.SetActive(false);
            for (int i = 0; i < EndCardNode.childCount; i++)
            {
                Destroy(EndCardNode.GetChild(i).gameObject);
            }
            GameEntry.Data.GetData<DataPlayer>().GetBoxAward(treasureBoxData.MoneyInfos, CurrencyEvent.ShopTreasureChest);
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            var hoerUpgradeData = GameEntry.Data.GetData<HeroUpgradeData>();
            long curCommonMaterial = dataPlayer.GetCurrency(103);
            currentCommonMaterialText.text = ((HugeNum)curCommonMaterial).ToString();
            currentCoinText.text = ((HugeNum)dataPlayer.GetCurrency(CurrencyType.Coin)).ToString();
            currentDiamondText.text = ((HugeNum)dataPlayer.GetCurrency(CurrencyType.Gem)).ToString();
            RefreshBtnState();
        }

        private void CloseForm()
        {
            Close();
        }
    }
}
